package action;

import beanInfo.MeaningInfo;
import http.Response;
import http.ResponseBuilder;
import model.ApiResultModel;
import service.DictionaryService;

import java.util.Map;

/**
 * 只负责接收参数不做任何处理转交给service层处理
 */
public class DictionaryAction {

    public ResponseBuilder responseBuilder = new ResponseBuilder();

    public Response add(Map<String, String> params) {
        long user_id = Long.valueOf(params.getOrDefault("user_id", "0"));
        int add_mode = Integer.valueOf(params.getOrDefault("add_mode", MeaningInfo.ADD_WORD + ""));
        String wordStr = params.getOrDefault("word_string", "");
        String meanings = params.getOrDefault("meanings", "");
        ApiResultModel apiResultModel = DictionaryService.add(user_id, add_mode, wordStr, meanings);
        return responseBuilder.buildResponseWithJsonBody(apiResultModel.json());
    }

    public Response delete_word(Map<String, String> params) {
        long user_id = Long.valueOf(params.getOrDefault("user_id", "0"));
        long word_id = Long.valueOf(params.getOrDefault("word_id", "0"));
        ApiResultModel apiResultModel = DictionaryService.deleteWord(user_id, word_id);
        return responseBuilder.buildResponseWithJsonBody(apiResultModel.json());
    }

    public Response delete_meaning(Map<String, String> params) {
        long user_id = Long.valueOf(params.getOrDefault("user_id", "0"));
        long meaning_id = Long.valueOf(params.getOrDefault("meaning_id", "0"));
        ApiResultModel apiResultModel = DictionaryService.deleteMeaning(user_id, meaning_id);
        return responseBuilder.buildResponseWithJsonBody(apiResultModel.json());
    }

    public Response search_word(Map<String, String> params) {
        long word_id = Long.valueOf(params.getOrDefault("word_id", "0"));
        String wordStr = params.getOrDefault("word_string", "");
        ApiResultModel apiResultModel = DictionaryService.searchWord(word_id, wordStr);
        return responseBuilder.buildResponseWithJsonBody(apiResultModel.json());
    }

    public Response get_new_words(Map<String, String> params) {
        int num = Integer.valueOf(params.getOrDefault("num", "10"));
        ApiResultModel apiResultModel = DictionaryService.getNewWords(num);
        return responseBuilder.buildResponseWithJsonBody(apiResultModel.json());
    }

    public Response update_meaning(Map<String, String> params) {
        long user_id = Long.valueOf(params.getOrDefault("user_id", "0"));
        long meaning_id = Long.valueOf(params.getOrDefault("meaning_id", "0"));
        String update_meaning = params.getOrDefault("update_meaning", "");
        ApiResultModel apiResultModel = DictionaryService.updateMeaning(user_id, meaning_id, update_meaning);
        return responseBuilder.buildResponseWithJsonBody(apiResultModel.json());
    }

    public Response get_search_hint(Map<String, String> params) {
        long user_id = Long.valueOf(params.getOrDefault("user_id", "0"));
        int num = Integer.valueOf(params.getOrDefault("num", "0"));
        String prefix = params.getOrDefault("prefix", "");
        ApiResultModel apiResultModel = DictionaryService.getSearchHint(prefix, num);
        return responseBuilder.buildResponseWithJsonBody(apiResultModel.json());
    }

    public Response get_random_word(Map<String, String> params) {
        long user_id = Long.valueOf(params.getOrDefault("user_id", "0"));
        ApiResultModel apiResultModel = DictionaryService.getRandomWord();
        return responseBuilder.buildResponseWithJsonBody(apiResultModel.json());
    }
}
